package com.iptv.grape.domain;

import com.google.gson.annotations.Expose;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.ui.control.ad.ListViewUI;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;

import java.util.ArrayList;
import java.util.List;

// 节目单预约对象
public class BillAppointment implements IListViewShowItem{
    // 当前被预约的频道
    @Expose(serialize = false, deserialize = false)
    private Channel channel;
    // 当前被预约的节目单
    @Expose(serialize = false, deserialize = false)
    private ChannelBill channelBill;
    // 频道ID
    @Expose(serialize = true, deserialize = true)
    private String channelKeyID;
    // 节目单ID
    @Expose(serialize = true, deserialize = true)
    private String channelBillKeyID;

    // 是否取消
    @Expose(serialize = true, deserialize = true)
    private boolean isCancel = false;

    public boolean isCancel() {
        return isCancel;
    }

    public void setCancel(boolean cancel) {
        isCancel = cancel;
    }

    // 是否为空
    public boolean isEmpty(){
        return this.channel == null || this.channelBill == null;
    }

    // 根据 频道ID 和 节目单ID 来获得频道对象 和 节目单对象
    public boolean init(){
        try {
            this.channel = GrapeContext.curGrapeContext().findChannelByID(channelKeyID);
            if (this.channel == null) {
                return false;
            }
            this.channelBill = GrapeContext.curGrapeContext().getBillList().findChannelBillByID(channelBillKeyID);
            if (this.channelBill == null) {
                return false;
            }
            if (this.channelBill.getBillType().equals("future")) {
                this.channelBill.setAppointment(true);
                return true;
            }
        }
        catch(Exception e){
            e.printStackTrace();
            return false;
        }
        return false;
    }



    public BillAppointment(){

    }

    public BillAppointment(Channel channel, ChannelBill channelBill) {
        this.channel = channel;
        this.channelBill = channelBill;
        this.channelKeyID = channel.getKeyID();
        this.channelBillKeyID = channelBill.getKeyID();
        AppLog.getCurLog().out(this.channelBillKeyID );
    }

    public String getChannelKeyID() {
        return channelKeyID;
    }

    public void setChannelKeyID(String channelKeyID) {
        this.channelKeyID = channelKeyID;
    }

    public String getChannelBillKeyID() {
        return channelBillKeyID;
    }

    public void setChannelBillKeyID(String channelBillKeyID) {
        this.channelBillKeyID = channelBillKeyID;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public ChannelBill getChannelBill() {
        return channelBill;
    }

    public void setChannelBill(ChannelBill channelBill) {
        this.channelBill = channelBill;
    }

    @Override
    public String getCode() {
        return channel.getCode() + "_" + channelBill.getCode();
    }

    @Override
    public int getSort() {
        return 0;
    }

    @Override
    public String getText() {
        return channelBill.getText();
    }

    @Override
    public ListViewStyle getListViewStyle() {


        if(AppConstant.isTvStb()) {
            return new ListViewStyle(214, "billAppointment", "BillAppointment",
                    5, 2, 0, 86, null);
        }
        else {
            return new ListViewStyle(214, "billAppointment", "BillAppointment",
                    7, 3, 0, 86, null);
        }
    }

    @Override
    public List<IListViewShowItem> getChList() {
        return new ArrayList<IListViewShowItem>();
    }

    @Override
    public void itemClick(List<IListViewShowItem> dataSouce, int pos, ListViewUI listViewUI) {
        // 取消预约
        if(this.channel != null && this.channelBill != null){
            GrapeContext.curGrapeContext().getCurActivity().showChannelBill(this.channel,this.channelBill,this.channelBill.isAppointment(),listViewUI);
        }
    }
}
